package initcation

import (
	"day12_12_homework/config"
	"day12_12_homework/internal/models/dao"
	"encoding/json"
)

func FindUserAndFriend(friendId int) string {
	var friend *dao.Friend
	err = config.DB.Model(&dao.Friend{}).Where("friend_id=? || my_id=?", friendId, friendId).Limit(1).Find(&friend).Error
	if err != nil {
		return "你们没加好友"
	}
	if friend.ID == 0 {
		return "你们没加好友"
	}
	return ""
}

func CreateMessage(data []byte, sendId, readId int) string {
	var message *MessageChat
	json.Unmarshal(data, &message)
	messages := &dao.Message{
		SendId:   sendId,
		ReadId:   readId,
		Contents: message.Contents,
	}
	friend := FindUserAndFriend(readId)
	if friend != "" {
		return "好友不存在"
	} else {
		config.DB.Model(&dao.Message{}).Create(&messages)
	}
	return ""
}
